From e060c24b767a34a674bec7112c22558b505f92a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 23 Mar 2021 19:19:14 +0200 Subject: [PATCH] [PATCH] h2645parser: Catch overflows in AVC/HEVC NAL unit length calculations Offset and size are stored as 32 bit guint and might overflow when adding the nal_length_size, so let's avoid that. For the size this would happen if the AVC/HEVC NAL unit size happens to be stored in 4 bytes and is 4294967292 or higher, which is likely corrupted data anyway. For the offset this is something for the caller of these functions to take care of but is unlikely to happen as it would require parsing on a >4GB buffer. Allowing these overflows causes all kinds of follow-up bugs in the h2645parse elements, ranging from infinite loops and memory leaks to potential memory corruptions. Part-of: Gbp-Pq: Name 0001-h2645parser-Catch-overflows-in-AVC-HEVC-NAL-unit-length.patch --- gst-libs/gst/codecparsers/gsth264parser.c | 16 +++++++++++++++- gst-libs/gst/codecparsers/gsth265parser.c | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/gst-libs/gst/codecparsers/gsth264parser.c b/gst-libs/gst/codecparsers/gsth264parser.c index b65b90f..cb51a45 100644 --- a/gst-libs/gst/codecparsers/gsth264parser.c +++ b/gst-libs/gst/codecparsers/gsth264parser.c @@ -1397,6 +1397,14 @@ gst_h264_parser_identify_nalu_avc (GstH264NalParser * nalparser, memset (nalu, 0, sizeof (*nalu)); + /* Would overflow guint below otherwise: the callers needs to ensure that + * this never happens */ + if (offset > G_MAXUINT32 - nal_length_size) { + GST_WARNING ("offset + nal_length_size overflow"); + nalu->size = 0; + return GST_H264_PARSER_BROKEN_DATA; + } + if (size < offset + nal_length_size) { GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT ", offset %u", size, offset); @@ -1411,7 +1419,13 @@ gst_h264_parser_identify_nalu_avc (GstH264NalParser * nalparser, nalu->sc_offset = offset; nalu->offset = offset + nal_length_size; - if (size < nalu->size + nal_length_size) { + if (nalu->size > G_MAXUINT32 - nal_length_size) { + GST_WARNING ("NALU size + nal_length_size overflow"); + nalu->size = 0; + return GST_H264_PARSER_BROKEN_DATA; + } + + if (size < (gsize) nalu->size + nal_length_size) { nalu->size = 0; return GST_H264_PARSER_NO_NAL_END; diff --git a/gst-libs/gst/codecparsers/gsth265parser.c b/gst-libs/gst/codecparsers/gsth265parser.c index cf7f71f..c59b58b 100644 --- a/gst-libs/gst/codecparsers/gsth265parser.c +++ b/gst-libs/gst/codecparsers/gsth265parser.c @@ -1274,6 +1274,14 @@ gst_h265_parser_identify_nalu_hevc (GstH265Parser * parser, memset (nalu, 0, sizeof (*nalu)); + /* Would overflow guint below otherwise: the callers needs to ensure that + * this never happens */ + if (offset > G_MAXUINT32 - nal_length_size) { + GST_WARNING ("offset + nal_length_size overflow"); + nalu->size = 0; + return GST_H265_PARSER_BROKEN_DATA; + } + if (size < offset + nal_length_size) { GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT ", offset %u", size, offset); @@ -1288,7 +1296,13 @@ gst_h265_parser_identify_nalu_hevc (GstH265Parser * parser, nalu->sc_offset = offset; nalu->offset = offset + nal_length_size; - if (size < nalu->size + nal_length_size) { + if (nalu->size > G_MAXUINT32 - nal_length_size) { + GST_WARNING ("NALU size + nal_length_size overflow"); + nalu->size = 0; + return GST_H265_PARSER_BROKEN_DATA; + } + + if (size < (gsize) nalu->size + nal_length_size) { nalu->size = 0; return GST_H265_PARSER_NO_NAL_END; -- 2.30.2